#include "admin.h"

void update_admin(FILE *f) {

    char login[81];
    char pass[81];

    Admin new;

    printf("Entre com o login: \n");
    ler(login);

    fseek(f, 0, SEEK_SET);
    while (!feof(f)) {
        fread(&new, sizeof (Admin), 1, f);
        if (!strcmp(new.login, login)) {
            printf("login existe. Entre com a nova senha: \n");
            ler(pass);
            strcpy(new.pass, pass);
            fseek(f, -1 * sizeof (Admin), SEEK_CUR);
            fwrite(&new, sizeof (Admin), 1, f);
            break;
        }
    }
}

void create_admin(FILE *f) {

    Admin new;

    printf("digite o login:\n");
    ler(new.login);
    printf("digite a senha:\n");
    ler(new.pass);
    fseek(f, 0, SEEK_END);
    fwrite(&new, sizeof (Admin), 1, f);
}

void list_admin(FILE *f) {
    Admin aux;
    fseek(f, 0, SEEK_SET);
    fread(&aux, sizeof (Admin), 1, f);
    while (!feof(f)) {
        printf("Admin:%s \n Senha:%s \n ", aux.login, aux.pass);
        fread(&aux, sizeof (Admin), 1, f);

    }
}

void init_admin(FILE *f) {
    Admin new;

    strcpy(new.login, "admin");
    strcpy(new.pass, "1234");
    fseek(f, 0, SEEK_END);
    fwrite(&new, sizeof (Admin), 1, f);
}

int validation_admin(FILE *f) {
    char login[81];
    char pass[81];

    Admin new;


    printf("Entre com o login: \n");
    ler(login);
    printf("Entre com a senha: \n");
    ler(pass);

    
    fseek(f, 0, SEEK_SET);
    while (!feof(f)) {
        fread(&new, sizeof (Admin), 1, f);
        if ((!strcmp(new.login, login) && !strcmp(new.pass, pass))) {
            printf("login e senha válidos\n");
            return PROC_SUCCESS;
        }
    }
    //Se percorrer todo o arquivo e não encontrar
    printf("login ou senha invalidos\n");
    return PROC_FAILURE;
}